package popcorn.ticket.serviceimpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import popcorn.ticket.dao.CollectionsMapper;
import popcorn.ticket.entity.Collections;
import popcorn.ticket.service.CollectionsService;
import popcorn.ticket.tool.AjaxResponse;
import popcorn.ticket.tool.PageData;

@Service
public class CollectionsServiceImpl implements CollectionsService {
	@Autowired
	private CollectionsMapper cm;

	AjaxResponse ar = new AjaxResponse();

	// 根据电影id查询
	@Override
	public PageData<Collections> selectByMovieId(int movieId, int page, int limit) {
		// 调用插件
		PageHelper.startPage(page, limit);

		List<Collections> list = cm.selectByMovieId(movieId);
		PageInfo<Collections> pageInfo = new PageInfo<Collections>(list);

		PageData<Collections> pageData = new PageData<Collections>(pageInfo.getList(), page, (int) pageInfo.getTotal(),
				limit);
		pageData.setCode(0);
		pageData.setMsg("");

		return pageData;
	}

	// 添加收藏
	@Override
	public AjaxResponse insert(Collections record) {

		int n = cm.insert(record);
		// 判断
		if (n > 0) {
			ar.setCode(200);
			ar.setMsg("添加成功");
			return ar;
		}
		ar.setCode(0);
		ar.setMsg("添加失败");
		return ar;
	}
	//删除收藏
	@Override
	public AjaxResponse deleteByPrimaryKey(int collectid) {
		
		int n = cm.deleteByPrimaryKey(collectid);
		// 判断
		if (n > 0) {
			ar.setCode(200);
			ar.setMsg("删除成功");
			return ar;
		}
		ar.setCode(0);
		ar.setMsg("删除失败");
		return ar;
	}

	@Override
	public List<Collections> selectByUserId(int userid) {
		// TODO Auto-generated method stub
		return cm.selectByUserId(userid);
	}
	
	/**
	 * 收藏榜
	 */
	@Override
	public PageData<Collections> getList(int page) {
		// 开启分页
		PageHelper.startPage(page, 20);
		// 获取收藏榜单数据
		List<Collections> list = cm.getList();
		// pageinfo收集分页数据
		PageInfo<Collections> pageInfo = new PageInfo<>(list);
		// 实例化对象
		PageData<Collections> pd = new PageData<>(list, page, (int) pageInfo.getTotal(), 20);
		pd.setCode(0);
		pd.setMsg("");

		return pd;
	}
	
	// 根据用户id和电影id删除收藏
	@Override
	public AjaxResponse deleteByUMId(Collections record) {
		// TODO Auto-generated method stub

		int n = cm.deleteByUMId(record);

		// 判断
		if (n > 0) {
			ar.setCode(200);
			ar.setMsg("删除成功");
			return ar;
		}
		ar.setCode(0);
		ar.setMsg("删除失败");
		return ar;
	}
    //用于判断用户是否收藏该电影
	@Override
	public int docoll(Collections record) {
		// TODO Auto-generated method stub
		return cm.docoll(record);
	}
}
